const storage = {
  set (key, value) {
    if (typeof value === 'object') {
      value = JSON.stringify(value)
    }
    localStorage.setItem(key, value)
  },
  get (key) {
    let data, obj
    try {
      obj = localStorage.getItem(key)
      if (typeof obj === 'string') {
        data = JSON.parse(obj)
      } else {
        data = obj
      }
    } catch (e) {
      data = obj
    }
    return data
  },
  remove (key) {
    localStorage.removeItem(key)
  },
  clear () {
    localStorage.clear()
  }
}

export default storage
